from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.core.config import settings
from app.core.database import create_tables
from app.routers import example_router

# 创建FastAPI应用
app = FastAPI(
    title=settings.APP_NAME,
    description="一个简单的全栈应用模板，支持数据库操作",
    version=settings.VERSION,
    debug=settings.DEBUG
)

# 添加跨域中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=settings.get_allowed_origins(),
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 创建数据库表
@app.on_event("startup")
async def startup_event():
    """应用启动时的事件"""
    create_tables()

# 注册路由
app.include_router(example_router)

@app.get("/", summary="健康检查")
def root():
    """健康检查端点"""
    return {
        "message": f"{settings.APP_NAME}运行正常", 
        "status": "healthy",
        "version": settings.VERSION
    }

@app.get("/api/hello")
def hello():
    """示例API端点"""
    return {"message": "Hello from FastAPI!"}

